package com.alibaba.springboot.health.controller;

import com.alibaba.springboot.health.pojo.Order;
import com.alibaba.springboot.health.service.OrderService;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * (Order)表控制层
 *
 * @author makejava
 * @since 2019-12-24 14:33:36
 */
@Controller
@RequestMapping("/order")
public class OrderController {
    private static final Logger LOGGER= LogManager.getLogger(OrderController.class);
    /**
     * 服务对象
     */
    @Autowired
    private OrderService orderService;



    /**
     * 通过主键查询单条数据
     *all
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("/searchOne")
    @ResponseBody
    public Order searchOne(Integer id) {
        return orderService.queryById(id);
    }



    /**
     * zdj
     * 权限  后台用户  orderadmin
     * 查询多条数据
     * @param page 页码
     * @param pageSize 页大小
     * @param order 实体
     * @return PageInfo<Order> 分页数据集合
     */
    @RequiresPermissions(value = {"/order/searchAll"})
    @RequestMapping("searchAll")
    public String  searchAll(@RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "pageSize",defaultValue = "5")int pageSize, Order order, Model model){
        PageInfo<Order> orders = orderService.queryAll(page, pageSize, order);
        LOGGER.info("======================================="+orders);
        model.addAttribute("orders",orders);
        return "order-list";
    }

    /**
     * xl
     * @param order
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity addOrder(Order order){
        if (order!= null){
            Order order1 = orderService.insert(order);
            return new ResponseEntity(HttpStatus.OK);
        }
        return new ResponseEntity(HttpStatus.BAD_REQUEST);
    }

}